#include "settingsmanager.h"
#include <QtCore/QStandardPaths>
#include <QtCore/QDir>
#include <QtCore/QDebug>

SettingsManager::SettingsManager(QObject *parent)
    : QObject(parent)
    , m_settings(new QSettings("DDM", "DownloadManager", this))
    , m_maxConcurrentDownloads(5) // 默认5个并发下载以提高速度
    , m_downloadSpeedLimit(0)     // 默认无速度限制
    , m_useProxy(false)
    , m_proxyPort(8080)
    , m_maxRetries(3)             // 默认最大重试3次
    , m_showNotificationOnComplete(true)
    , m_startDownloadOnAdd(false)
{
    // 设置默认下载路径
    QString downloadPath = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
    if (downloadPath.isEmpty()) {
        downloadPath = QDir::currentPath();
    }
    m_defaultDownloadPath = downloadPath;
    
    loadSettings();
}

SettingsManager::~SettingsManager()
{
    saveSettings();
}

// 下载设置
int SettingsManager::maxConcurrentDownloads() const
{
    return m_maxConcurrentDownloads;
}

void SettingsManager::setMaxConcurrentDownloads(int max)
{
    m_maxConcurrentDownloads = max;
}

int SettingsManager::downloadSpeedLimit() const
{
    return m_downloadSpeedLimit;
}

void SettingsManager::setDownloadSpeedLimit(int limit)
{
    m_downloadSpeedLimit = limit;
}

QString SettingsManager::defaultDownloadPath() const
{
    return m_defaultDownloadPath;
}

void SettingsManager::setDefaultDownloadPath(const QString &path)
{
    m_defaultDownloadPath = path;
}

bool SettingsManager::useProxy() const
{
    return m_useProxy;
}

void SettingsManager::setUseProxy(bool use)
{
    m_useProxy = use;
}

QString SettingsManager::proxyHost() const
{
    return m_proxyHost;
}

void SettingsManager::setProxyHost(const QString &host)
{
    m_proxyHost = host;
}

int SettingsManager::proxyPort() const
{
    return m_proxyPort;
}

void SettingsManager::setProxyPort(int port)
{
    m_proxyPort = port;
}

// 重试设置
int SettingsManager::maxRetries() const
{
    return m_maxRetries;
}

void SettingsManager::setMaxRetries(int maxRetries)
{
    m_maxRetries = maxRetries;
}

// 用户界面设置
bool SettingsManager::showNotificationOnComplete() const
{
    return m_showNotificationOnComplete;
}

void SettingsManager::setShowNotificationOnComplete(bool show)
{
    m_showNotificationOnComplete = show;
}

bool SettingsManager::startDownloadOnAdd() const
{
    return m_startDownloadOnAdd;
}

void SettingsManager::setStartDownloadOnAdd(bool start)
{
    m_startDownloadOnAdd = start;
}

void SettingsManager::saveSettings()
{
    // 下载设置
    m_settings->setValue("downloads/maxConcurrent", m_maxConcurrentDownloads);
    m_settings->setValue("downloads/speedLimit", m_downloadSpeedLimit);
    m_settings->setValue("downloads/defaultPath", m_defaultDownloadPath);
    m_settings->setValue("downloads/useProxy", m_useProxy);
    m_settings->setValue("downloads/proxyHost", m_proxyHost);
    m_settings->setValue("downloads/proxyPort", m_proxyPort);
    
    // 重试设置
    m_settings->setValue("downloads/maxRetries", m_maxRetries);
    
    // 用户界面设置
    m_settings->setValue("ui/showNotifications", m_showNotificationOnComplete);
    m_settings->setValue("ui/startOnAdd", m_startDownloadOnAdd);
    
    m_settings->sync();
}

void SettingsManager::loadSettings()
{
    // 下载设置
    m_maxConcurrentDownloads = m_settings->value("downloads/maxConcurrent", m_maxConcurrentDownloads).toInt();
    m_downloadSpeedLimit = m_settings->value("downloads/speedLimit", m_downloadSpeedLimit).toInt();
    m_defaultDownloadPath = m_settings->value("downloads/defaultPath", m_defaultDownloadPath).toString();
    m_useProxy = m_settings->value("downloads/useProxy", m_useProxy).toBool();
    m_proxyHost = m_settings->value("downloads/proxyHost", m_proxyHost).toString();
    m_proxyPort = m_settings->value("downloads/proxyPort", m_proxyPort).toInt();
    
    // 重试设置
    m_maxRetries = m_settings->value("downloads/maxRetries", m_maxRetries).toInt();
    
    // 用户界面设置
    m_showNotificationOnComplete = m_settings->value("ui/showNotifications", m_showNotificationOnComplete).toBool();
    m_startDownloadOnAdd = m_settings->value("ui/startOnAdd", m_startDownloadOnAdd).toBool();
}